#!/usr/bin/env python3

# 鼠标点击选取左上角 再次点击选取右下角区域截图保存在../pic/img_write.png
import sys
import time
import numpy as np
import cv2 as cv

ix, iy = -1, -1
count = 0


def draw_circle(event, x, y, flags, param):
    if event == cv.EVENT_LBUTTONDOWN:
        global ix, iy, count
        count += 1
        if count == 1:
            ix, iy = x, y
            print(ix, iy)
        if count == 2:
            img_write = image[iy:y, ix:x]
            cv.imshow("img_write", img_write)
            cv.imwrite("mario_coin.png", img_write)
            sys.exit(0)


image = cv.imread("../pic/mario.png")
cv.namedWindow("image", cv.WINDOW_NORMAL)
cv.setMouseCallback('image', draw_circle)
cv.imshow("image", image)

cv.waitKey(0)
cv.destroyAllWindows()
